<template>
  <div class="btns">
    <el-button @click="open" v-bind="$attrs" v-if="showBtn">
      <slot></slot>
    </el-button>
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="visible"
      @close="onClose"
      v-if="visible"
      width="600px"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        size="small"
        v-loading="loading"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="form.roleName"></el-input>
        </el-form-item>
        <el-form-item size="mini" prop="menuIds" label="授权">
          <el-tree
            :data="menuList"
            :props="menuListTreeProps"
            node-key="id"
            ref="menuListTree"
            @check="onCheck"
            show-checkbox
          >
          </el-tree>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="handleSubmit" size="small" type="primary"
          >确 定</el-button
        >
        <el-button @click="visible = false" size="small">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  props: {
    showBtn: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      menuList: [],
      menuListTreeProps: {
        label: "menuName",
        children: "list"
      },
      form: {
        roleName: "",
        roleDesc: "",
        status: 0,
        menuIds: []
        // checkedIds: ""
      },
      rules: {
        roleName: [
          { required: true, message: "角色名称不能为空", trigger: "blur" }
        ],
        menuIds: [{ required: true, message: "请设置角色权限" }]
      },
      loading: false,
      visible: false,
      edit: false,
      title: "新增"
    };
  },
  methods: {
    onClose() {
      // this.form = defaultForm()
    },
    async open({ title, form, params, edit }) {
      this.form.roleName = "";
      if (this.$refs.form) this.$refs.form.clearValidate();
      this.visible = true;
      await this.getRoles();
    },
    async handleSubmit() {
      this.form.menuIds = this.$refs.menuListTree
        .getCheckedKeys()
        .concat(this.$refs.menuListTree.getHalfCheckedKeys());
      this.$refs.form.validate(async valid => {
        if (valid) {
          this.loading = true;
          let [err] = await this.$Api.getApi("roles").add({
              data: { ...this.form, type: 1 }
            });
          this.loading = false;
          if (err) return;
          this.handleSuccess("操作成功！");
          this.visible = false;
          this.$emit("refresh");
        }
      });
    },
    onCheck(node, { checkedKeys, halfCheckedKeys }) {
      this.form.menuIds = checkedKeys;
      this.$refs.form.validateField("menuIds");
    },
    async getRoles() {
      this.form.menuIds = [];
      let [err, res] = await this.$Api.Menus.list();
      await this.$nextTick();
      this.$refs["form"].resetFields();
      this.$refs.menuListTree.setCheckedKeys([]);
      if (!err) this.menuList = res;
    }
  }
};
</script>
